####################################################################################
## 合并污染系数评估
export work_dir_batch1=~/20220915_gastric_multiple/dna_batch_raw
export work_dir_batch2=~/20220915_gastric_multiple/dna_batch1

cat ${work_dir_batch1}/results/Qc/Summary_contamination.table \
> ${Qc_path}/Summary_contamination.table
cat ${work_dir_batch2}/results/Qc/Summary_contamination.table | grep -v Sample \
>> ${Qc_path}/Summary_contamination.table

## 污染样本
cat ${Qc_path}/Summary_contamination.table | grep -v Sample | awk -F'\t' '{if($2 > 0.2)print $1}' \
> ${Qc_path}/ContaminationSample.list

conSample=`cat ${Qc_path}/ContaminationSample.list | tr '\n' '|' | sed 's/|$//'`

####################################################################################
## 分析样本的list
## 去除污染的样本
## 提取存在配对IM和GC的样本，只提取IM、IGC和DGC
cat ${config_path}/Tumor_Normal_DNA.tsv | head -1 > ${config_path}/tumor_normal.list
cat ${config_path}/Tumor_Normal_DNA.tsv | grep -E -w -v ${conSample} |\
awk -F'\t' '{OFS="\t"}{if($6~"IM")print}' | \
awk -F'\t' '{OFS="\t"}{if(($4=="IM" || $4=="IGC" || $4=="DGC") && $6!="IM + MIX" )print}' \
>> ${config_path}/tumor_normal.list

## 拷贝一份
cp ${config_path}/tumor_normal.list ${config_path}/tumor_normal.bk.list
cp ${config_path}/tumor_normal.list ${config_path}/tumor_normal.class.list
cp ${config_path}/tumor_normal.list ${config_path}/tumor_normal.class.bk.list

####################################################################################
## 拷贝文件
echo -e "Sample\tPurity\tPloidy" > ${Titan_path}/Purity_titan.final.tsv
echo -e "Normal\tTumor\tTotal_Number_of_Sites\tNumber_of_Somatic_Sites\tRate" > ${Qc_path}/All_Msi.tsv
echo -e "Tumor,Normal,Raw_Muts,SimpleRepeats_QC,SegDup_QC,SV_QC,Sex_Qc" > ${Qc_path}/Vcf_QC.list
echo -e "Tumor\tcoverage" > ${Qc_path}/Burden.coverage10x.Autosomal.cds.txt
echo -e "Tumor\tcoverage" > ${Qc_path}/Burden.coverage10x.Autosomal.txt
echo -e "Tumor\tNormal\tTumorDepth\tNormalDepth" > ${Qc_path}/Depth_summary.txt
## qualimap统计深度包含了重复reads
echo -e "Tumor\tNormal\tTumorDepth\tNormalDepth" > ${Qc_path}/Depth_summary.qualimap.txt

for line in `cat ${config_path}/tumor_normal.bk.list | awk '{OFS=","}{print $3,$2}' | grep -v Tumor`
do

echo $line

Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`

#######################################
## 纯度
cat ${work_dir_batch1}/results/titanCombine/Purity_titan.final.tsv ${work_dir_batch2}/results/titanCombine/Purity_titan.final.tsv |\
grep -E -w ${Tumor} >> ${Titan_path}/Purity_titan.final.tsv

## Seg单独的
file_name=`find ${work_dir_batch1}/results/titanCombine/FinalModel ${work_dir_batch2}/results/titanCombine/FinalModel |\
grep -E /${Tumor}_ | grep ichor.seg.txt`
cp -f ${file_name} ${Titan_path}/FinalModel/${Tumor}_${Normal}.seg

#######################################
## MSI
cat ${work_dir_batch1}/results/Qc/All_Msi.tsv ${work_dir_batch2}/results/Qc/All_Msi.tsv |\
grep -E -w ${Tumor} >> ${Qc_path}/All_Msi.tsv

#######################################
## 突变数量
cat ${work_dir_batch1}/results/Qc/Vcf_QC.list ${work_dir_batch2}/results/Qc/Vcf_QC.list |\
grep -E -w ${Tumor} >> ${Qc_path}/Vcf_QC.list

#######################################
## cds区域超过10x覆盖的区域
cat ${work_dir_batch1}/results/Qc/Burden.coverage10x.Autosomal.cds.txt ${work_dir_batch2}/results/Qc/Burden.coverage10x.Autosomal.cds.txt |\
grep -E -w ${Tumor} >> ${Qc_path}/Burden.coverage10x.Autosomal.cds.txt

#######################################
## 总的超过10x覆盖的区域
cat ${work_dir_batch1}/results/Qc/Burden.coverage10x.Autosomal.txt ${work_dir_batch2}/results/Qc/Burden.coverage10x.Autosomal.txt |\
grep -E -w ${Tumor} >> ${Qc_path}/Burden.coverage10x.Autosomal.txt

#######################################
## 测序深度
cat ${work_dir_batch1}/results/Qc/Depth_summary.txt ${work_dir_batch2}/results/Qc/Depth_summary.txt |\
grep -E -w ${Tumor} >> ${Qc_path}/Depth_summary.txt

cat ${work_dir_batch1}/results/Qc/Depth_summary.qualimap.txt ${work_dir_batch2}/results/Qc/Depth_summary.qualimap.txt |\
grep -E -w ${Tumor} >> ${Qc_path}/Depth_summary.qualimap.txt

#######################################
## 突变
## vcf
file_name=`find ${work_dir_batch1}/results/vcf/ ${work_dir_batch2}/results/vcf/ |\
grep -E ${Tumor}_${Normal}_QC.vcf `
cp -f ${file_name} ${vcf_path}/

file_name=`find ${work_dir_batch1}/results/vcf/ ${work_dir_batch2}/results/vcf/ |\
grep -E ${Tumor}_${Normal}_GGA_Filter.vcf `
cp -f ${file_name} ${vcf_path}/

## maf
file_name=`find ${work_dir_batch1}/results/maf/ ${work_dir_batch2}/results/maf/ |\
grep -E ${Tumor}_${Normal}_QC.maf `
cp -f ${file_name} ${maf_path}/

file_name=`find ${work_dir_batch1}/results/maf/ ${work_dir_batch2}/results/maf/ |\
grep -E ${Tumor}_${Normal}_GGA_Filter_funcotated.maf `
cp -f ${file_name} ${maf_path}/

## treeomics
file_name=`find ${work_dir_batch1}/results/tree/ ${work_dir_batch2}/results/tree/ |\
grep -E Combine_${Normal}_GGA_PASS.vcf `
cp -f ${file_name} ${tree_path}/

done


####################################################################################
## bam文件放到一个目录下
cd ${bam_path}

## Normal
for sample in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk -F'\t' '{print $2}' | sort -u`
do
for file in `find /public/home/xxf2019/20220915_gastric_multiple/dna_batch1/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch2/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch_raw/results/bam/ | grep "/"${sample}-recal`
do
ln -snf ${file}
done
done

## Tumor
for sample in `cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -v Normal | awk -F'\t' '{print $3}' | sort -u`
do
for file in `find /public/home/xxf2019/20220915_gastric_multiple/dna_batch1/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch2/results/bam/ \
/public/home/xxf2019/20220915_gastric_multiple/dna_batch_raw/results/bam/ | grep "/"${sample}-recal`
do
ln -snf ${file}
done
done

####################################################################################
## JZGCWES16、JZ585T3和JZGCWES153
## 患者其它TP53所在区域均为LOH，患者该处为Loss或gain，修改Seg单独的
## S9该患者的DNAH3另外IGC为LOH，该处为GAIN
## JZGCWES138该患者的NRG1为Netural，另外IGC为GAIN

cp ${Titan_path}/FinalModel/JZGCWES16_JZGCWES0693.seg ${Titan_path}/FinalModel/JZGCWES16_JZGCWES0693.raw.seg 
cp ${Titan_path}/FinalModel/JZGCWES153_JZGCWES0908.seg ${Titan_path}/FinalModel/JZGCWES153_JZGCWES0908.raw.seg 
cp ${Titan_path}/FinalModel/JZ585T3_JZ585B.seg ${Titan_path}/FinalModel/JZ585T3_JZ585B.raw.seg 
cp ${Titan_path}/FinalModel/S9_S57.seg ${Titan_path}/FinalModel/S9_S57.raw.seg 
cp ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.seg ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.raw.seg 
cp ${Titan_path}/FinalModel/JZ645T_JZ645B.seg ${Titan_path}/FinalModel/JZ645T_JZ645B.raw.seg


## TP53
cat ${Titan_path}/FinalModel/JZGCWES16_JZGCWES0693.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="DLOH" && $2==17){$10=2;$11="NLOH";$12=2;$14=2;$20=2;$21="NEUT";$22=2};
print
}' > ${Titan_path}/FinalModel/JZGCWES16_JZGCWES0693.seg 

## TP53
cat ${Titan_path}/FinalModel/JZ585T3_JZ585B.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="GAIN" && $2==17){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/JZ585T3_JZ585B.seg 

## TP53
cat ${Titan_path}/FinalModel/JZGCWES153_JZGCWES0908.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="DLOH" && $2==17){$10=2;$11="NLOH";$12=2;$14=2;$20=2;$21="NEUT";$22=2};
print
}' > ${Titan_path}/FinalModel/JZGCWES153_JZGCWES0908.seg 

##
cat ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="ALOH" && $2==17){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.seg 

cat ${Titan_path}/FinalModel/JZGCWES139_JZGCWES731.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="ALOH" && $2==17){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/JZGCWES139_JZGCWES731.seg 
##

cat ${Titan_path}/FinalModel/JZGCWES139_JZGCWES731.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="ALOH" && $2==17){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/JZGCWES139_JZGCWES731.seg 
##

cat ${Titan_path}/FinalModel/JZ645T_JZ645B.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="ALOH" && $2==17){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/JZ645T_JZ645B.seg 


## DNAH3
cat ${Titan_path}/FinalModel/S9_S57.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="GAIN" && $2==16){$10=2;$11="NLOH";$12=2;$13=0;$14=2;$20=2;$21="NEUT";$22=2;$23=0};
print
}' > ${Titan_path}/FinalModel/S9_S57.seg 

## NRG1
cat ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.raw.seg  | 
awk -F'\t' '
{OFS="\t"}
{
if($11=="HET" && $2==8){$10=5;$11="GAIN";$12=3;$13=1;$14=2;$20=3;$21="GAIN";$22=2;$23=1};
print
}' > ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.seg 

rm -rf ${Titan_path}/FinalModel/JZGCWES153_JZGCWES0908.raw.seg 
rm -rf ${Titan_path}/FinalModel/JZGCWES16_JZGCWES0693.raw.seg 
rm -rf ${Titan_path}/FinalModel/JZ585T3_JZ585B.raw.seg 
rm -rf ${Titan_path}/FinalModel/S9_S57.raw.seg
rm -rf ${Titan_path}/FinalModel/JZGCWES138_JZGCWES731.raw.seg 
rm -rf ${Titan_path}/FinalModel/JZGCWES139_JZGCWES731.raw.seg
rm -rf ${Titan_path}/FinalModel/JZ645T_JZ645B.raw.seg

## 拷贝数
cat ${work_dir_batch1}/results/titanCombine/Titan_all_seg.final.tsv | head -1 > ${Titan_path}/Titan_all_seg.final.tsv
for line in `cat ${config_path}/tumor_normal.bk.list | awk '{OFS=","}{print $3,$2}' | grep -v Tumor`
do
echo $line
Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`
## Seg
cat ${Titan_path}/FinalModel/${Tumor}_${Normal}.seg |\
grep -E -w ${Tumor}_${Normal} >> ${Titan_path}/Titan_all_seg.final.tsv
done

